/*
 * Created by JFormDesigner on Fri Jun 28 18:23:27 CST 2024
 */

package scct.hys.ui;


import scct.hys.dao.CinemaDao;
import scct.hys.dao.MovieDao;
import scct.hys.dao.UserDao;
import scct.hys.model.Movie;
import scct.hys.model.User;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 26984
 */
public class PanelMovie extends JPanel {
    FormMain main;
    DefaultTableModel model;
    DefaultTableModel Searchmodel = null;

    public PanelMovie(FormMain main) {
        initComponents();
        this.main = main;
        initView();

    }

    private void initView() {
        model = (DefaultTableModel) tabMovie.getModel();
        model.addColumn("id");
        model.addColumn("电影名称");
        model.addColumn("导演");
        model.addColumn("主演");
        model.addColumn("上映时间");
        model.addColumn("电影时长");
        getTable();

//        MovieDao movieDao = new MovieDao();
//        List<Movie> allMove = movieDao.getAllMovies();
//        for (Movie movie : allMove) {
//            cboMovie.addItem(movie.getName());
//
//        }

    }

    private void getTable() {
        model.setRowCount(0);
        MovieDao movieDao = new MovieDao();

        List<Movie> allMovies = movieDao.getAllMovies();
        for (Movie allMovie : allMovies) {
            model.addRow(new Object[]{allMovie.getId(), allMovie.getName(), allMovie.getDirector(), allMovie.getActors(), allMovie.getReleaseDate(), allMovie.getDuration()});
        }
        System.out.println(allMovies);
    }

    private void btnAdd(ActionEvent e) {

        // TODO add your code here
        DlgMovieAdd dlgMovieAdd = new DlgMovieAdd(main);
        dlgMovieAdd.setModal(true);
        dlgMovieAdd.setVisible(true);
        getTable();

    }

    private void btnUpdate(ActionEvent e) {
        // TODO add your code here
        if (tabMovie.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "请选择修改对象");
            return;
        }
        int index = tabMovie.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
        DlgMovieUpdate dlgMovieUpdate = new DlgMovieUpdate(main, id);
        dlgMovieUpdate.setModal(true);
        dlgMovieUpdate.setVisible(true);
        getTable();

    }

    private void btnDel(ActionEvent e) {
        // TODO add your code here

        if (tabMovie.getSelectedRowCount() == 0) {
            JOptionPane.showMessageDialog(this, "删除成功");
            return;
        }
        int index = tabMovie.getSelectedRow();
        int id = Integer.parseInt(model.getValueAt(index, 0).toString());
        DlgMovieDel dlgMovieDel = new DlgMovieDel(main, id);
        dlgMovieDel.setModal(true);
        dlgMovieDel.setVisible(true);
        getTable();
    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here

        tabMovie.setModel(model);
        Searchmodel = new DefaultTableModel();
        Searchmodel.addColumn("id");
        Searchmodel.addColumn("电影名称");
        Searchmodel.addColumn("导演");
        Searchmodel.addColumn("主演");
        Searchmodel.addColumn("上映时间");
        Searchmodel.addColumn("电影时长");
        Searchmodel.setRowCount(0);
        String searchkey = txtSearchKey.getText();
        System.out.println(searchkey);
        String searchlist = (String) cboMovie.getSelectedItem();
        System.out.println(searchlist);
        int row = tabMovie.getRowCount();
        System.out.println("row:" + row);
        int column = tabMovie.getColumnCount();
        System.out.println("column" + column);
        for (int i = 0; i < row; i++) {
            if (tabMovie.getRowCount() < row) {
            } else {

                for (int j = 0; j < column; j++) {
                    if (Objects.equals(searchlist, tabMovie.getColumnName(j))) {
                        System.out.println(0002);
                        System.out.println("单元格的值:" + tabMovie.getValueAt(i, j));
                        String cellvalue = tabMovie.getValueAt(i, j).toString().toLowerCase();
                        System.out.println(cellvalue.contains(searchkey));
                        if (cellvalue.contains(searchkey)) {

                            Object[] rowDate = new Object[tabMovie.getColumnCount()];
                            for (int k = 0; k < tabMovie.getColumnCount(); k++) {
                                rowDate[k] = tabMovie.getValueAt(i, k);
                                System.out.println(rowDate[k]);
                            }
                            Searchmodel.addRow(rowDate);
                            tabMovie.setRowSelectionAllowed(true);
                            break;
                        }
                    }

                }
            }

        }
        tabMovie.setModel(Searchmodel);
//        String text=txtSearchKey.getText();
//        ArrayList<Movie> movies=new MovieDao().getMovieByName(text);
//        model.setRowCount(0);
//        for (Movie movie:movies){
//            Vector v=new Vector();
//            v.add(movie.getId());
//            v.add(movie.getName());
//            v.add(movie.getDirector());
//            v.add(movie.getActors());
//            v.add(movie.getReleaseDate());
//            v.add(movie.getDuration());
//            model.addRow(v);
//        }

    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 韩月生
        panel1 = new JPanel();
        btnAdd = new JButton();
        btnDel = new JButton();
        btnUpdate = new JButton();
        cboMovie = new JComboBox<>();
        txtSearchKey = new JTextField();
        btnSearch = new JButton();
        scrollPane1 = new JScrollPane();
        tabMovie = new JTable();

        //======== this ========
        setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new javax . swing.
        border .EmptyBorder ( 0, 0 ,0 , 0) ,  "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border . TitledBorder. CENTER
        ,javax . swing. border .TitledBorder . BOTTOM, new java. awt .Font ( "Dia\u006cog", java .awt . Font
        . BOLD ,12 ) ,java . awt. Color .red ) , getBorder () ) );  addPropertyChangeListener(
        new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (java . beans. PropertyChangeEvent e) { if( "\u0062ord\u0065r"
        .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } );
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 5));

            //---- btnAdd ----
            btnAdd.setText("\u6dfb\u52a0\u5f71\u7247");
            btnAdd.addActionListener(e -> btnAdd(e));
            panel1.add(btnAdd);

            //---- btnDel ----
            btnDel.setText("\u5220\u9664");
            btnDel.addActionListener(e -> btnDel(e));
            panel1.add(btnDel);

            //---- btnUpdate ----
            btnUpdate.setText("\u66f4\u6539");
            btnUpdate.addActionListener(e -> btnUpdate(e));
            panel1.add(btnUpdate);

            //---- cboMovie ----
            cboMovie.setModel(new DefaultComboBoxModel<>(new String[] {
                "id",
                "\u5f71\u7247\u540d\u79f0",
                "\u5bfc\u6f14",
                "\u4e3b\u6f14",
                "\u4e0a\u6620\u65f6\u95f4",
                "\u7535\u5f71\u65f6\u957f"
            }));
            panel1.add(cboMovie);

            //---- txtSearchKey ----
            txtSearchKey.setColumns(7);
            panel1.add(txtSearchKey);

            //---- btnSearch ----
            btnSearch.setText("\u67e5\u627e");
            btnSearch.addActionListener(e -> btnSearch(e));
            panel1.add(btnSearch);
        }
        add(panel1, BorderLayout.NORTH);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabMovie);
        }
        add(scrollPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 韩月生
    private JPanel panel1;
    private JButton btnAdd;
    private JButton btnDel;
    private JButton btnUpdate;
    private JComboBox<String> cboMovie;
    private JTextField txtSearchKey;
    private JButton btnSearch;
    private JScrollPane scrollPane1;
    private JTable tabMovie;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
